﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Secams.controller;
using Secams.model;
using Secams.utils;

namespace Secams.form
{
    public partial class FormLogin : Form
    {

        protected bool _isAdmin = false;

        ParticipantDao _participantDao = new ParticipantDao(AppContext.getInstance().dbFilePath);
        protected ParticipantInfo _participantInfo = new ParticipantInfo();

        public FormLogin(ScenariosCollection scenariosCollection)
        {
            this.Font = AppContext.getDefaultFont(this.Font);
            InitializeComponent();

            foreach (ScenarioInstanceSequence scenarioSeq in scenariosCollection.getScenarioSequences())
            {
                cboScenarioSeq.Items.Add(scenarioSeq.name);
            }
            cboScenarioSeq.Focus();
        }
        
        public bool isAdmin
        {
            get { return _isAdmin; }
        }

        public string id
        {
            get { return this.txtId.Text; }
        }

        public string getSelectedScenarioSequene()
        {
            return (string)this.cboScenarioSeq.Text;
        }

        public ParticipantInfo getParticipantInfo()
        {
            return _participantInfo;
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            
        }

        protected bool validateFields()
        {
            bool retval = true;
            if (!ControlUtils.checkComboBox(ref this.cboScenarioSeq, ref lblVersion))
                retval = false;
            if (!ControlUtils.checkTextBox(ref this.txtId, ref lblId))
                retval = false;
            return retval;
         
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {

            if (this.txtPassword.Text.Length > 0)
            {
                if (SignOnManager.ROLE_ADMIN.Equals(SignOnManager.getInstance().signIn(this.txtId.Text, this.txtPassword.Text)))
                {
                    _isAdmin = true;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.lblMessage.Text = "Invalid password";
                }
            }
            else
            {
                if (validateFields())
                {

                    if (txtId.Text.Trim().Length > 0)
                    {
                        _participantInfo = _participantDao.get(txtId.Text.Trim());
                        if (_participantInfo == null)
                        {
                            _participantInfo = new ParticipantInfo();
                            _participantInfo.id = txtId.Text.Trim();
                        }
                    }


                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
        }
    }
}
